import { MockMethod } from 'vite-plugin-mock'

// 模拟用户数据
const users = [
  {
    id: 1,
    login_name: 'admin',
    full_name: '系统管理员',
    department: '技术部',
    email: 'admin@example.com',
    status: 1,
    created_at: '2023-08-01 10:00:00',
    updated_at: '2023-08-01 10:00:00',
    roles: [1]
  },
  {
    id: 2,
    login_name: 'user1',
    full_name: '普通用户1',
    department: '市场部',
    email: 'user1@example.com',
    status: 1,
    created_at: '2023-08-01 10:00:00',
    updated_at: '2023-08-01 10:00:00',
    roles: [2]
  },
  {
    id: 3,
    login_name: 'user2',
    full_name: '普通用户2',
    department: '财务部',
    email: 'user2@example.com',
    status: 1,
    created_at: '2023-08-01 10:00:00',
    updated_at: '2023-08-01 10:00:00',
    roles: [2]
  }
]

export default [
  // 获取用户列表
  {
    url: '/users',
    method: 'get',
    response: (opt: any) => {
      const { page = 1, page_size = 10, keyword = '' } = opt.query
      
      let result = [...users]
      
      // 关键词过滤
      if (keyword) {
        const lowerKeyword = keyword.toLowerCase()
        result = result.filter(item => 
          item.login_name.toLowerCase().includes(lowerKeyword) || 
          item.full_name.toLowerCase().includes(lowerKeyword) ||
          item.department.toLowerCase().includes(lowerKeyword) ||
          item.email.toLowerCase().includes(lowerKeyword)
        )
      }
      
      // 分页
      const total = result.length
      const start = (page - 1) * page_size
      const end = Math.min(start + page_size, total)
      const paginatedResult = result.slice(start, end)
      
      return {
        code: 0,
        data: paginatedResult,
        total,
        errorMsg: ''
      }
    }
  },
  
  // 获取单个用户详情
  {
    url: '/users/:id',
    method: 'get',
    response: (opt: any) => {
      const { id } = opt.query
      const user = users.find(u => u.id === Number(id))
      
      if (user) {
        return {
          code: 0,
          data: user,
          errorMsg: ''
        }
      } else {
        return {
          code: 404,
          data: null,
          errorMsg: '用户不存在'
        }
      }
    }
  },
  
  // 创建用户
  {
    url: '/users',
    method: 'post',
    response: (opt: any) => {
      const { body } = opt
      const newId = Math.max(...users.map(u => u.id)) + 1
      const now = new Date().toISOString().split('T').join(' ').substring(0, 19)
      
      const newUser = {
        ...body,
        id: newId,
        created_at: now,
        updated_at: now,
        status: 1
      }
      
      users.push(newUser)
      
      return {
        code: 0,
        data: newUser,
        errorMsg: ''
      }
    }
  },
  
  // 更新用户
  {
    url: '/users/:id',
    method: 'put',
    response: (opt: any) => {
      const { id } = opt.query
      const { body } = opt
      const index = users.findIndex(u => u.id === Number(id))
      
      if (index !== -1) {
        const now = new Date().toISOString().split('T').join(' ').substring(0, 19)
        const updatedUser = {
          ...users[index],
          ...body,
          updated_at: now
        }
        
        users[index] = updatedUser
        
        return {
          code: 0,
          data: updatedUser,
          errorMsg: ''
        }
      } else {
        return {
          code: 404,
          data: null,
          errorMsg: '用户不存在'
        }
      }
    }
  },
  
  // 删除用户
  {
    url: '/users/:id',
    method: 'delete',
    response: (opt: any) => {
      const { id } = opt.query
      const index = users.findIndex(u => u.id === Number(id))
      
      if (index !== -1) {
        users.splice(index, 1)
        
        return {
          code: 0,
          data: null,
          errorMsg: ''
        }
      } else {
        return {
          code: 404,
          data: null,
          errorMsg: '用户不存在'
        }
      }
    }
  },
  
  // 重置密码
  {
    url: '/users/:id/reset-password',
    method: 'post',
    response: (opt: any) => {
      const { id } = opt.query
      const user = users.find(u => u.id === Number(id))
      
      if (user) {
        return {
          code: 0,
          data: null,
          errorMsg: ''
        }
      } else {
        return {
          code: 404,
          data: null,
          errorMsg: '用户不存在'
        }
      }
    }
  }
] as MockMethod[]
